package com.cloud.logger.zchain.core;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: wcy
 * Date: 2017/11/27
 * Time: 13:15
 */
public class AbstractFilter {

    public final static Set ignore = new HashSet(){{
        add("js");
        add("img");
        add("css");
        add("ico");
        add("png");
        add("jpg");
        add("gif");
    }};

    final static Pattern pattern = Pattern.compile("\\S*[?]\\S*");

    /**
     * 获取链接的后缀名
     * @return
     * @param url
     */
    public String parseSuffix(StringBuffer url) {

        Matcher matcher = pattern.matcher(url);

        String[] spUrl = url.toString().split("/");
        int len = spUrl.length;
        String endUrl = spUrl[len - 1];

        if(matcher.find()) {
            String[] spEndUrl = endUrl.split("\\?");
            return spEndUrl[0].split("\\.")[1];
        }
        return endUrl.split("\\.").length>1?endUrl.split("\\.")[1]:"";
    }

    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    private static Gson gson = new GsonBuilder().create();

    public String readJSONString(HttpServletRequest request) {
        String result = "";
        StringBuffer json = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                json.append(line);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        result =  json.toString();

        Map<String, String[]> ps=request.getParameterMap();
        if(result.length()>0  && ps.size()>0){
            //组装新params
            Map<String, String> params= Maps.newHashMap();

            for (Map.Entry<String, String[]> key : ps.entrySet()) {
                String[] value=key.getValue();
                if (value!=null&&value.length>0) {
                    params.put(key.getKey(), StringUtils.join(value));
                }
            }
            return gson.toJson(params);
        }

        return result;

    }

}
